package test.zhang;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import lombok.SneakyThrows;
import org.apache.commons.collections4.map.HashedMap;
import test.zhang.entity.MergeEntity;
import utils.SmallTool;
import utils.StringUtil;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

public class 给张哥的一个表pmidjcrzkynian {
    @SneakyThrows
    public static void main(String[] args) {

        Db use = Db.use();
        String fromBase = "basic_jour_merge";
        String data = "spider_pubmed_vip_extend";
        String toSave = "pmid_year_jcr_zky";
        SmallTool.printMemory();
        List<Entity> query = use.query("select nc_nlm_id,jcr_if,zky_large_min from " + fromBase);
        Map<String, MergeEntity> matchEntity = new HashedMap<>();
        for (Entity entity : query) {
            String nlmId = entity.getStr("nc_nlm_id");
            String jcr_if = entity.getStr("jcr_if");
            String zky_large_min = entity.getStr("zky_large_min");
            if (StringUtil.isNotNull(nlmId)) {
                MergeEntity mergeEntity = new MergeEntity();
                if (StringUtil.isNotNull(jcr_if)) {
                    mergeEntity.setJcr(Double.parseDouble(jcr_if));
                }
                mergeEntity.setZKY(zky_large_min);
                matchEntity.put(nlmId, mergeEntity);
            }
        }
        SmallTool.printMemory();

        Entity entity = use.queryOne("select max(pmid) as cout from " + data);
        Integer cout = entity.getInt("cout");
        IntStream.rangeClosed(1, cout).parallel().forEach(as -> {
            try {
                Entity entity1 = use.queryOne("select pmid ,pub_year, med_nlm_id from " + data + " where pmid = ?", as);
                if (entity1 != null) {
                    Integer pmid = entity1.getInt("pmid");
                    Integer year = entity1.getInt("pub_year");
                    String medNlmId = entity1.getStr("med_nlm_id");

                    if (StringUtil.isNotNull(medNlmId)) {
                        Entity ins = new Entity();
                        ins.setTableName(toSave);
                        ins.set("pmid", pmid);
                        ins.set("year", year);
                        ins.set("nlmid", medNlmId);
                        if (matchEntity.containsKey(medNlmId)) {
                            MergeEntity mergeEntity = matchEntity.get(medNlmId);
                            ins.set("jcr_if", mergeEntity.getJcr());
                            ins.set("zky_large_min", mergeEntity.getZKY());
                        }
                        use.insert(ins);
                    }

                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        });


    }
}


